//
//  SheelViewController.m
//  inquiry
//
//  Created by 高宇 on 2020/6/19.
//  Copyright © 2020 高宇. All rights reserved.
//

#import "SheelViewController.h"

@interface SheelViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic ,strong) UIView * backView;
@property (nonatomic, strong) UIPickerView *pickerView;
@end

@implementation SheelViewController

-(instancetype)init{
    self = [super init];
    if(self){
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor clearColor];
    
    UIImageView * backImageView = [[UIImageView alloc] init];
    backImageView.backgroundColor = [UIColor blackColor];
    backImageView.alpha = 0.5;
    [self.view addSubview:backImageView];
    backImageView.frame = self.view.frame;
    
    
    self.backView = [[UIView alloc] init];
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backView];
    self.backView.frame = CGRectMake(0, self.view.frame.size.height-180, self.view.frame.size.width, 180);
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(15, 8, 44, 26);
    [button setTitle:@"取消" forState:0];
    [button setTitleColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0] forState:0];
    [button addTarget:self action:@selector(sheelClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:button];
    
    UIButton *turebutton = [UIButton buttonWithType:UIButtonTypeCustom];
    turebutton.frame = CGRectMake(self.backView.frame.size.width-15-44, 8, 44, 26);
    [turebutton setTitle:@"完成" forState:0];
    [turebutton setTitleColor:[UIColor colorWithRed:110/255.0 green:132/255.0 blue:238/255.0 alpha:1.0] forState:0];
    [turebutton addTarget:self action:@selector(sheelTureClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backView addSubview:turebutton];
    
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, self.backView.frame.size.width, self.backView.frame.size.height-40)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    [self.backView addSubview:pickerView];
    self.pickerView = pickerView;
}


- (void)sheelClick:(UIButton *)button{
    if (self.block) {
        self.block();
    }
}

- (void)sheelTureClick:(UIButton *)button{
    if (self.tureblock) {
        self.tureblock(self.dic);
    }
}

#pragma Mark -- UIPickerViewDataSource

// pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

// pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.arr.count;
}

#pragma Mark -- UIPickerViewDelegate

// 返回选中的行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.dic = self.arr[row];
}

//返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSDictionary *dic = self.arr[row];
    self.dic = dic;
    return dic[@"name"];
}

- (void)setArr:(NSArray *)arr{
    if (arr != _arr) {
        _arr = arr;
        [self.pickerView reloadAllComponents];
    }
}

@end
